package com.handinglian.ai.callin.service.impl;

import com.github.pagehelper.PageHelper;
import com.handinglian.ai.callin.dto.CallInDetailDto;
import com.handinglian.ai.callin.dto.CallInDto;
import com.handinglian.ai.callin.entity.CallIn;
import com.handinglian.ai.callin.entity.CallInBusiness;
import com.handinglian.ai.callin.entity.CallInChat;
import com.handinglian.ai.callin.mapper.CallInMapper;
import com.handinglian.ai.callin.param.CallInCreateParam;
import com.handinglian.ai.callin.service.CallInBusinessService;
import com.handinglian.ai.callin.service.CallInChatService;
import com.handinglian.ai.callin.service.CallInService;
import com.handinglian.ai.common.dto.CommonPage;
import com.handinglian.ai.common.util.FastJsonUtil;
import com.handinglian.ai.common.util.GeneralUtil;
import com.handinglian.ai.record.dto.CallRecordDetailDto;
import com.handinglian.ai.record.service.CallRecordService;
import com.handinglian.ai.system.dto.AdvisoryDto;
import com.handinglian.ai.system.dto.AdvisoryPorportionDto;
import com.handinglian.ai.system.dto.AdvisoryTypeDto;
import com.handinglian.ai.system.dto.BusinessDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;

@Service
public class CallInServiceImpl implements CallInService {
    @Autowired
    private CallInMapper callInMapper;
    @Autowired
    private CallInBusinessService callInBusinessService;
    @Autowired
    private CallInChatService callInChatService;
    @Autowired
    private CallRecordService callRecordService;


    @Override
    @Transactional
    @Async
    public Future<Integer> create(CallIn callIn) {
        return new AsyncResult(callInMapper.insertSelective(callIn));
    }

    @Override
    public CommonPage<CallInDto> listPage(String callerNo, String phoneStatus, String sortField, String sortOrder, Integer pageNum, Integer pageSize) {
        List<Integer> phoneStatuses = GeneralUtil.stringToIntegerList(phoneStatus);
        PageHelper.startPage(pageNum, pageSize);
        List<CallInDto> callInDtos = callInMapper.list(callerNo, phoneStatuses, sortField, sortOrder);
        return CommonPage.restPage(callInDtos);
    }

    @Override
    public CallInDetailDto getDetail(String callid) {
        List<CallInBusiness> callInBusinesses = callInBusinessService.list(callid);
        StringBuffer business = new StringBuffer();
        callInBusinesses.forEach(callInBusiness -> {
            business.append(callInBusiness.getBusiness()).append(",");
        });
        List<CallInChat> callInChats = callInChatService.list(callid);
        CallRecordDetailDto detailDto = callRecordService.getDetail(callid);

        CallInDetailDto callInDetailDto = new CallInDetailDto();
        callInDetailDto.setBusiness(business.toString());
        callInDetailDto.setCallRecordDetailDto(detailDto);
        callInDetailDto.setCallInChats(callInChats);
        return callInDetailDto;
    }

    @Override
    public AdvisoryDto getAdvisory() {
        //咨询用户数
        Integer advisoryUserNum = callInMapper.countDistinctCallerNo();
        //咨询业务数
        Integer advisoryNum = callInMapper.count();
        BigDecimal perCapitaNum = new BigDecimal(advisoryNum).divide(new BigDecimal(advisoryUserNum), 2, BigDecimal.ROUND_HALF_UP);
        List<BusinessDto> businessDtos = callInMapper.listBusiness();

        AdvisoryDto advisoryDto = new AdvisoryDto();
        advisoryDto.setAdvisoryUserNum(advisoryUserNum);
        advisoryDto.setAdvisoryPerCapita(perCapitaNum);
        advisoryDto.setBusinessDtos(businessDtos);
        return advisoryDto;
    }

    @Override
    public AdvisoryPorportionDto getAdvisoryProportion() {
        Integer advisoryAllNum = callInMapper.count();
        List<AdvisoryTypeDto> advisoryTypeDtos = callInMapper.listAdvisoryType();
        advisoryTypeDtos.forEach(advisoryTypeDto -> {
            BigDecimal rate = new BigDecimal(advisoryTypeDto.getAdvisoryNum()).divide(new BigDecimal(advisoryAllNum),2, BigDecimal.ROUND_HALF_UP);
            advisoryTypeDto.setBusinessRate(rate);
        });

        AdvisoryPorportionDto advisoryPorportionDto = new AdvisoryPorportionDto();
        advisoryPorportionDto.setAdvisoryAllNum(advisoryAllNum);
        advisoryPorportionDto.setAdvisoryTypeDtos(advisoryTypeDtos);
        return advisoryPorportionDto;
    }
}
